<template>
  <div class="cart">
    <h4>全部商品</h4>
    <div class="cart-main">
      <div class="cart-th">
        <div class="cart-th1">全部</div>
        <div class="cart-th2">商品</div>
        <div class="cart-th3">单价（元）</div>
        <div class="cart-th4">数量</div>
        <div class="cart-th5">小计（元）</div>
        <div class="cart-th6">操作</div>
      </div>
      <div class="cart-body">
        <ul class="cart-list" v-for="cart in cartList" :key="cart.id">
          <li class="cart-list-con1">
            <input
              type="checkbox"
              name="chk_list"
              :checked="cart.isChecked"
              @change="changeChecked(cart, 1 - cart.isChecked)"
            />
          </li>
          <li class="cart-list-con2">
            <!-- 图片渲染 -->
            <img :src="cart.imgUrl" />
            <div class="item-msg">
              <!-- 描述渲染 -->
              {{ cart.skuName }}
            </div>
          </li>
          <li class="cart-list-con4">
            <!-- 价格渲染 -->
            <span class="price">{{ cart.skuPrice }}</span>
          </li>
          <li class="cart-list-con5">
            <!-- 计数器 -->
            <!-- 给记数器传value值  input  相当于v-modle 拆开的 $event是当组件触发后后返回的值  -->
            <!-- $event 代表回调的第一个返回值 -->
            <InputNumber
              :value="cart.skuNum"
              @input="setSkuNum(cart, $event)"
            />
          </li>
          <li class="cart-list-con6">
            <!-- 总价 -->
            <span class="sum">{{ cart.skuNum * cart.skuPrice }}</span>
          </li>
          <li class="cart-list-con7">
            <button  @click="delSelectValue(cart.skuId)">删除</button>
            <br />
          </li>
        </ul>

        <!--   <ul class="cart-list">
          <li class="cart-list-con1">
            <input type="checkbox" name="chk_list" id="" value="" />
          </li>
          <li class="cart-list-con2">
            <img src="./images/goods2.png" />
            <div class="item-msg">
              华为（MIJIA） 华为metaPRO 30 浴霸4摄像 超清晰
            </div>
          </li>
          <li class="cart-list-con4">
            <span class="price">5622.00</span>
          </li>
          <li class="cart-list-con5">
            <a href="javascript:void(0)" class="mins">-</a>
            <input
              autocomplete="off"
              type="text"
              value="1"
              minnum="1"
              class="itxt"
            />
            <a href="javascript:void(0)" class="plus">+</a>
          </li>
          <li class="cart-list-con6">
            <span class="sum">5622</span>
          </li>
          <li class="cart-list-con7">
            <a href="#none" class="sindelet">删除</a>
            <br />
          </li>
        </ul> -->
        <!-- > -->
      </div>
    </div>
    <div class="cart-tool">
      <div class="select-all">
        <input class="chooseAll" type="checkbox" v-model="allChecked" />
        <span>全选</span>
      </div>
      <div class="option">
        <a  @click="delAllIsChecked">删除选中的商品</a>
      </div>
      <div class="money-box">
        <div class="chosed">
          已选择 <span>{{ selectedValue }}</span
          >件商品
        </div>
        <div class="sumprice">
          <em>总价（不含运费） ：</em>
          <i class="summoney">{{ totalPrice }}</i>
        </div>
        <div class="sumbtn">
          <router-link to="/trade">结算</router-link>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import debounce from "lodash/debounce";
import InputNumber from "@/components/InputNumber";
import {
  reqGetCartList,
  reqAddToCart,
  reqIsCheckCart,
  reqDeleteCart
} from "@/api/axios/addToCart";

export default {
  name: "ShopCart",
  components: {
    InputNumber,
  },
  data() {
    return {
      cartList: [],
    };
  },
   mounted() {
     this.reqCarList()
  },
  methods: {
    async reqCarList(){
     const result = await reqGetCartList();
       console.log(result);
      this.cartList = result[0] ? result[0].cartInfoList : [];
     },


    // 防止多点触发请求。进行防抖
    setSkuNum: debounce(async function (cart, skuNum) {
      let num = skuNum - cart.skuNum;
      // 请求添加购物车，在服务端里添加对应的id ,更改数量
      await reqAddToCart(cart.skuId, num);
      // 在数据里 赋值最新的数据（数量）
      cart.skuNum = skuNum;
    }, 300),
      
    async changeChecked(cart, isChecked) {
      // console.log(cart,isChecked);
      cart.isChecked = isChecked;
      await reqIsCheckCart(cart.skuId, isChecked);
      //  cart.isChecked = isChecked
    },
    
    // 删除
    async delSelectValue(skuId){


    //  confirm('确定要删除吗')
        
      await reqDeleteCart(skuId)
      this.reqCarList()
    },
    // 删除选中
     delAllIsChecked (){
       this.cartList.find((item)=>{ 
        if(item.isChecked){
         this.delSelectValue(item.skuId)
        } 
          })
         
         
    }
  },
  computed: {
    // 计算已选中的值
    selectedValue() {
      return this.cartList.reduce((a, b) => {
        return a + (b.isChecked ? 1 : 0);
      }, 0);
    },
    /* 计算总价 */
    totalPrice() {
      return this.cartList.reduce((next, current) => {
        return (
          next + (current.isChecked ? current.skuNum * current.skuPrice : 0)
        );
      }, 0);
    },
    /* 全选 */
    allChecked: {
      get() {
        return this.cartList.every((item) => {
          return item.isChecked;
        });
      },
      /* 反选 */
      set(newVal) {
        let isChecked = newVal ? 1 : 0;
        this.cartList.forEach((cart) => {
          if (cart.isChecked === isChecked) return;
          this.changeChecked(cart, isChecked);
        });
      },
    },
  },
  // 计算总价
};
</script>

<style lang="less" scoped>
.cart {
  width: 1200px;
  margin: 0 auto;
  h4 {
    margin: 9px 0;
    font-size: 14px;
    line-height: 21px;
  }
  .cart-main {
    .cart-th {
      background: #f5f5f5;
      border: 1px solid #ddd;
      padding: 10px;
      overflow: hidden;
      & > div {
        float: left;
      }
      .cart-th1 {
        width: 25%;
        input {
          vertical-align: middle;
        }
        span {
          vertical-align: middle;
        }
      }
      .cart-th2 {
        width: 25%;
      }
      .cart-th3,
      .cart-th4,
      .cart-th5,
      .cart-th6 {
        width: 12.5%;
      }
    }
    .cart-body {
      margin: 15px 0;
      border: 1px solid #ddd;
      .cart-list {
        padding: 10px;
        border-bottom: 1px solid #ddd;
        overflow: hidden;
        & > li {
          float: left;
        }
        .cart-list-con1 {
          width: 15%;
        }
        .cart-list-con2 {
          width: 35%;
          img {
            width: 82px;
            height: 82px;
            float: left;
          }
          .item-msg {
            float: left;
            width: 150px;
            margin: 0 10px;
            line-height: 18px;
          }
        }
        .cart-list-con4 {
          width: 10%;
        }
        .cart-list-con5 {
          width: 17%;
          .mins {
            border: 1px solid #ddd;
            border-right: 0;
            float: left;
            color: #666;
            width: 6px;
            text-align: center;
            padding: 8px;
            text-decoration: none;
          }
          input {
            border: 1px solid #ddd;
            width: 40px;
            height: 31px;
            float: left;
            text-align: center;
            font-size: 14px;
          }

          .plus {
            border: 1px solid #ddd;
            border-left: 0;
            float: left;
            color: #666;
            width: 6px;
            text-align: center;
            padding: 8px;
            text-decoration: none;
          }
        }

        .cart-list-con6 {
          width: 10%;

          .sum {
            font-size: 16px;
          }
        }

        .cart-list-con7 {
          width: 13%;

          a {
            color: #666;
          }
        }
      }
    }
  }
  .cart-tool {
    overflow: hidden;
    border: 1px solid #ddd;

    .select-all {
      padding: 10px;
      overflow: hidden;
      float: left;

      span {
        vertical-align: middle;
      }

      input {
        vertical-align: middle;
      }
    }

    .option {
      padding: 10px;
      overflow: hidden;
      float: left;

      a {
        float: left;
        padding: 0 10px;
        color: #666;
      }
    }

    .money-box {
      float: right;

      .chosed {
        line-height: 26px;
        float: left;
        padding: 0 10px;
      }

      .sumprice {
        width: 200px;
        line-height: 22px;
        float: left;
        padding: 0 10px;

        .summoney {
          color: #c81623;
          font-size: 16px;
        }
      }

      .sumbtn {
        float: right;
        a {
          display: block;
          position: relative;
          width: 96px;
          height: 52px;
          line-height: 52px;
          color: #fff;
          text-align: center;
          font-size: 18px;
          font-family: "Microsoft YaHei";
          background: #e1251b;
          overflow: hidden;
          text-decoration: none;
          &hover {
            color: white !important;
          }
        }
      }
    }
  }
}
</style>
